import pandas as pd
Filter the population of Boca Raton from ages 15 and up. | Create a DF that will consolidate the age groups to ">30 , 30-49 , 50+"
df=pd.read_csv('Florida.csv')
df.head(32)
| Label (Grouping) | Boca Raton city, Florida!!Total!!Estimate | Boca Raton city, Florida!!Total!!Margin of Error | Boca Raton city, Florida!!Percent!!Estimate | Boca Raton city, Florida!!Percent!!Margin of Error | Boca Raton city, Florida!!Male!!Estimate | Boca Raton city, Florida!!Male!!Margin of Error | Boca Raton city, Florida!!Percent Male!!Estimate | Boca Raton city, Florida!!Percent Male!!Margin of Error | Boca Raton city, Florida!!Female!!Estimate | Boca Raton city, Florida!!Female!!Margin of Error | Boca Raton city, Florida!!Percent Female!!Estimate | Boca Raton city, Florida!!Percent Female!!Margin of Error | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | Total population | 95,785 | ±30 | (X) | (X) | 42,417 | ±2,822 | (X) | (X) | 53,368 | ±2,822 | (X) | (X) |
| 1 | AGE | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 2 | Under 5 years | 4,233 | ±1,351 | 4.4% | ±1.4 | 1,840 | ±812 | 4.3% | ±1.9 | 2,393 | ±1,037 | 4.5% | ±1.9 |
| 3 | 5 to 9 years | 3,890 | ±1,289 | 4.1% | ±1.3 | 2,240 | ±997 | 5.3% | ±2.3 | 1,650 | ±659 | 3.1% | ±1.3 |
| 4 | 10 to 14 years | 4,335 | ±1,119 | 4.5% | ±1.2 | 2,158 | ±866 | 5.1% | ±2.0 | 2,177 | ±730 | 4.1% | ±1.3 |
| 5 | 15 to 19 years | 7,354 | ±1,588 | 7.7% | ±1.7 | 2,469 | ±942 | 5.8% | ±2.1 | 4,885 | ±1,467 | 9.2% | ±2.6 |
| 6 | 20 to 24 years | 6,916 | ±2,310 | 7.2% | ±2.4 | 3,351 | ±1,920 | 7.9% | ±4.3 | 3,565 | ±1,384 | 6.7% | ±2.5 |
| 7 | 25 to 29 years | 5,718 | ±1,531 | 6.0% | ±1.6 | 2,605 | ±983 | 6.1% | ±2.2 | 3,113 | ±1,106 | 5.8% | ±2.1 |
| 8 | 30 to 34 years | 5,374 | ±1,378 | 5.6% | ±1.4 | 2,122 | ±830 | 5.0% | ±2.0 | 3,252 | ±966 | 6.1% | ±1.8 |
| 9 | 35 to 39 years | 3,579 | ±1,119 | 3.7% | ±1.2 | 1,242 | ±623 | 2.9% | ±1.4 | 2,337 | ±791 | 4.4% | ±1.5 |
| 10 | 40 to 44 years | 3,908 | ±1,243 | 4.1% | ±1.3 | 2,061 | ±785 | 4.9% | ±1.8 | 1,847 | ±791 | 3.5% | ±1.5 |
| 11 | 45 to 49 years | 5,216 | ±1,125 | 5.4% | ±1.2 | 2,391 | ±707 | 5.6% | ±1.8 | 2,825 | ±804 | 5.3% | ±1.5 |
| 12 | 50 to 54 years | 6,458 | ±1,307 | 6.7% | ±1.4 | 2,174 | ±653 | 5.1% | ±1.5 | 4,284 | ±1,169 | 8.0% | ±2.2 |
| 13 | 55 to 59 years | 6,725 | ±1,558 | 7.0% | ±1.6 | 3,296 | ±940 | 7.8% | ±2.2 | 3,429 | ±1,058 | 6.4% | ±2.0 |
| 14 | 60 to 64 years | 8,173 | ±1,498 | 8.5% | ±1.6 | 3,655 | ±999 | 8.6% | ±2.3 | 4,518 | ±869 | 8.5% | ±1.7 |
| 15 | 65 to 69 years | 5,102 | ±1,116 | 5.3% | ±1.2 | 2,400 | ±713 | 5.7% | ±1.7 | 2,702 | ±729 | 5.1% | ±1.4 |
| 16 | 70 to 74 years | 5,715 | ±1,260 | 6.0% | ±1.3 | 2,866 | ±844 | 6.8% | ±1.9 | 2,849 | ±908 | 5.3% | ±1.7 |
| 17 | 75 to 79 years | 5,629 | ±1,595 | 5.9% | ±1.7 | 2,649 | ±725 | 6.2% | ±1.7 | 2,980 | ±1,490 | 5.6% | ±2.7 |
| 18 | 80 to 84 years | 3,283 | ±1,124 | 3.4% | ±1.2 | 1,261 | ±642 | 3.0% | ±1.5 | 2,022 | ±826 | 3.8% | ±1.6 |
| 19 | 85 years and over | 4,177 | ±1,472 | 4.4% | ±1.5 | 1,637 | ±626 | 3.9% | ±1.5 | 2,540 | ±1,269 | 4.8% | ±2.3 |
| 20 | SELECTED AGE CATEGORIES | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 21 | 5 to 14 years | 8,225 | ±1,996 | 8.6% | ±2.1 | 4,398 | ±1,535 | 10.4% | ±3.5 | 3,827 | ±1,017 | 7.2% | ±1.9 |
| 22 | 15 to 17 years | 2,653 | ±837 | 2.8% | ±0.9 | 1,317 | ±504 | 3.1% | ±1.1 | 1,336 | ±632 | 2.5% | ±1.2 |
| 23 | Under 18 years | 15,111 | ±2,318 | 15.8% | ±2.4 | 7,555 | ±1,777 | 17.8% | ±3.9 | 7,556 | ±1,460 | 14.2% | ±2.7 |
| 24 | 18 to 24 years | 11,617 | ±2,631 | 12.1% | ±2.7 | 4,503 | ±2,112 | 10.6% | ±4.7 | 7,114 | ±1,903 | 13.3% | ±3.3 |
| 25 | 15 to 44 years | 32,849 | ±2,938 | 34.3% | ±3.1 | 13,850 | ±2,362 | 32.7% | ±4.4 | 18,999 | ±2,350 | 35.6% | ±3.8 |
| 26 | 16 years and over | 82,579 | ±2,362 | 86.2% | ±2.5 | 35,710 | ±2,872 | 84.2% | ±3.9 | 46,869 | ±3,040 | 87.8% | ±2.7 |
| 27 | 18 years and over | 80,674 | ±2,317 | 84.2% | ±2.4 | 34,862 | ±2,786 | 82.2% | ±3.9 | 45,812 | ±2,989 | 85.8% | ±2.7 |
| 28 | 21 years and over | 74,111 | ±2,825 | 77.4% | ±3.0 | 33,212 | ±2,702 | 78.3% | ±4.2 | 40,899 | ±2,579 | 76.6% | ±3.4 |
| 29 | 60 years and over | 32,079 | ±3,764 | 33.5% | ±3.9 | 14,468 | ±1,747 | 34.1% | ±3.9 | 17,611 | ±3,137 | 33.0% | ±5.4 |
| 30 | 62 years and over | 28,218 | ±3,572 | 29.5% | ±3.7 | 12,558 | ±1,681 | 29.6% | ±3.7 | 15,660 | ±3,029 | 29.3% | ±5.2 |
| 31 | 65 years and over | 23,906 | ±3,570 | 25.0% | ±3.7 | 10,813 | ±1,626 | 25.5% | ±3.7 | 13,093 | ±3,044 | 24.5% | ±5.3 |
df.loc[5]
Label (Grouping) 15 to 19 years Boca Raton city, Florida!!Total!!Estimate 7,354 Boca Raton city, Florida!!Total!!Margin of Error ±1,588 Boca Raton city, Florida!!Percent!!Estimate 7.7% Boca Raton city, Florida!!Percent!!Margin of Error ±1.7 Boca Raton city, Florida!!Male!!Estimate 2,469 Boca Raton city, Florida!!Male!!Margin of Error ±942 Boca Raton city, Florida!!Percent Male!!Estimate 5.8% Boca Raton city, Florida!!Percent Male!!Margin of Error ±2.1 Boca Raton city, Florida!!Female!!Estimate 4,885 Boca Raton city, Florida!!Female!!Margin of Error ±1,467 Boca Raton city, Florida!!Percent Female!!Estimate 9.2% Boca Raton city, Florida!!Percent Female!!Margin of Error ±2.6 Name: 5, dtype: object
df.loc[6]
Label (Grouping) 20 to 24 years Boca Raton city, Florida!!Total!!Estimate 6,916 Boca Raton city, Florida!!Total!!Margin of Error ±2,310 Boca Raton city, Florida!!Percent!!Estimate 7.2% Boca Raton city, Florida!!Percent!!Margin of Error ±2.4 Boca Raton city, Florida!!Male!!Estimate 3,351 Boca Raton city, Florida!!Male!!Margin of Error ±1,920 Boca Raton city, Florida!!Percent Male!!Estimate 7.9% Boca Raton city, Florida!!Percent Male!!Margin of Error ±4.3 Boca Raton city, Florida!!Female!!Estimate 3,565 Boca Raton city, Florida!!Female!!Margin of Error ±1,384 Boca Raton city, Florida!!Percent Female!!Estimate 6.7% Boca Raton city, Florida!!Percent Female!!Margin of Error ±2.5 Name: 6, dtype: object
df.loc[7]
Label (Grouping) 25 to 29 years Boca Raton city, Florida!!Total!!Estimate 5,718 Boca Raton city, Florida!!Total!!Margin of Error ±1,531 Boca Raton city, Florida!!Percent!!Estimate 6.0% Boca Raton city, Florida!!Percent!!Margin of Error ±1.6 Boca Raton city, Florida!!Male!!Estimate 2,605 Boca Raton city, Florida!!Male!!Margin of Error ±983 Boca Raton city, Florida!!Percent Male!!Estimate 6.1% Boca Raton city, Florida!!Percent Male!!Margin of Error ±2.2 Boca Raton city, Florida!!Female!!Estimate 3,113 Boca Raton city, Florida!!Female!!Margin of Error ±1,106 Boca Raton city, Florida!!Percent Female!!Estimate 5.8% Boca Raton city, Florida!!Percent Female!!Margin of Error ±2.1 Name: 7, dtype: object
df.loc[8]
Label (Grouping) 30 to 34 years Boca Raton city, Florida!!Total!!Estimate 5,374 Boca Raton city, Florida!!Total!!Margin of Error ±1,378 Boca Raton city, Florida!!Percent!!Estimate 5.6% Boca Raton city, Florida!!Percent!!Margin of Error ±1.4 Boca Raton city, Florida!!Male!!Estimate 2,122 Boca Raton city, Florida!!Male!!Margin of Error ±830 Boca Raton city, Florida!!Percent Male!!Estimate 5.0% Boca Raton city, Florida!!Percent Male!!Margin of Error ±2.0 Boca Raton city, Florida!!Female!!Estimate 3,252 Boca Raton city, Florida!!Female!!Margin of Error ±966 Boca Raton city, Florida!!Percent Female!!Estimate 6.1% Boca Raton city, Florida!!Percent Female!!Margin of Error ±1.8 Name: 8, dtype: object
df.loc[9]
Label (Grouping) 35 to 39 years Boca Raton city, Florida!!Total!!Estimate 3,579 Boca Raton city, Florida!!Total!!Margin of Error ±1,119 Boca Raton city, Florida!!Percent!!Estimate 3.7% Boca Raton city, Florida!!Percent!!Margin of Error ±1.2 Boca Raton city, Florida!!Male!!Estimate 1,242 Boca Raton city, Florida!!Male!!Margin of Error ±623 Boca Raton city, Florida!!Percent Male!!Estimate 2.9% Boca Raton city, Florida!!Percent Male!!Margin of Error ±1.4 Boca Raton city, Florida!!Female!!Estimate 2,337 Boca Raton city, Florida!!Female!!Margin of Error ±791 Boca Raton city, Florida!!Percent Female!!Estimate 4.4% Boca Raton city, Florida!!Percent Female!!Margin of Error ±1.5 Name: 9, dtype: object
df.loc[10]
Label (Grouping) 40 to 44 years Boca Raton city, Florida!!Total!!Estimate 3,908 Boca Raton city, Florida!!Total!!Margin of Error ±1,243 Boca Raton city, Florida!!Percent!!Estimate 4.1% Boca Raton city, Florida!!Percent!!Margin of Error ±1.3 Boca Raton city, Florida!!Male!!Estimate 2,061 Boca Raton city, Florida!!Male!!Margin of Error ±785 Boca Raton city, Florida!!Percent Male!!Estimate 4.9% Boca Raton city, Florida!!Percent Male!!Margin of Error ±1.8 Boca Raton city, Florida!!Female!!Estimate 1,847 Boca Raton city, Florida!!Female!!Margin of Error ±791 Boca Raton city, Florida!!Percent Female!!Estimate 3.5% Boca Raton city, Florida!!Percent Female!!Margin of Error ±1.5 Name: 10, dtype: object
df.loc[11]
Label (Grouping) 45 to 49 years Boca Raton city, Florida!!Total!!Estimate 5,216 Boca Raton city, Florida!!Total!!Margin of Error ±1,125 Boca Raton city, Florida!!Percent!!Estimate 5.4% Boca Raton city, Florida!!Percent!!Margin of Error ±1.2 Boca Raton city, Florida!!Male!!Estimate 2,391 Boca Raton city, Florida!!Male!!Margin of Error ±707 Boca Raton city, Florida!!Percent Male!!Estimate 5.6% Boca Raton city, Florida!!Percent Male!!Margin of Error ±1.8 Boca Raton city, Florida!!Female!!Estimate 2,825 Boca Raton city, Florida!!Female!!Margin of Error ±804 Boca Raton city, Florida!!Percent Female!!Estimate 5.3% Boca Raton city, Florida!!Percent Female!!Margin of Error ±1.5 Name: 11, dtype: object
df.loc[12]
Label (Grouping) 50 to 54 years Boca Raton city, Florida!!Total!!Estimate 6,458 Boca Raton city, Florida!!Total!!Margin of Error ±1,307 Boca Raton city, Florida!!Percent!!Estimate 6.7% Boca Raton city, Florida!!Percent!!Margin of Error ±1.4 Boca Raton city, Florida!!Male!!Estimate 2,174 Boca Raton city, Florida!!Male!!Margin of Error ±653 Boca Raton city, Florida!!Percent Male!!Estimate 5.1% Boca Raton city, Florida!!Percent Male!!Margin of Error ±1.5 Boca Raton city, Florida!!Female!!Estimate 4,284 Boca Raton city, Florida!!Female!!Margin of Error ±1,169 Boca Raton city, Florida!!Percent Female!!Estimate 8.0% Boca Raton city, Florida!!Percent Female!!Margin of Error ±2.2 Name: 12, dtype: object
df.loc[13]
Label (Grouping) 55 to 59 years Boca Raton city, Florida!!Total!!Estimate 6,725 Boca Raton city, Florida!!Total!!Margin of Error ±1,558 Boca Raton city, Florida!!Percent!!Estimate 7.0% Boca Raton city, Florida!!Percent!!Margin of Error ±1.6 Boca Raton city, Florida!!Male!!Estimate 3,296 Boca Raton city, Florida!!Male!!Margin of Error ±940 Boca Raton city, Florida!!Percent Male!!Estimate 7.8% Boca Raton city, Florida!!Percent Male!!Margin of Error ±2.2 Boca Raton city, Florida!!Female!!Estimate 3,429 Boca Raton city, Florida!!Female!!Margin of Error ±1,058 Boca Raton city, Florida!!Percent Female!!Estimate 6.4% Boca Raton city, Florida!!Percent Female!!Margin of Error ±2.0 Name: 13, dtype: object
df.loc[14]
Label (Grouping) 60 to 64 years Boca Raton city, Florida!!Total!!Estimate 8,173 Boca Raton city, Florida!!Total!!Margin of Error ±1,498 Boca Raton city, Florida!!Percent!!Estimate 8.5% Boca Raton city, Florida!!Percent!!Margin of Error ±1.6 Boca Raton city, Florida!!Male!!Estimate 3,655 Boca Raton city, Florida!!Male!!Margin of Error ±999 Boca Raton city, Florida!!Percent Male!!Estimate 8.6% Boca Raton city, Florida!!Percent Male!!Margin of Error ±2.3 Boca Raton city, Florida!!Female!!Estimate 4,518 Boca Raton city, Florida!!Female!!Margin of Error ±869 Boca Raton city, Florida!!Percent Female!!Estimate 8.5% Boca Raton city, Florida!!Percent Female!!Margin of Error ±1.7 Name: 14, dtype: object
under30_pop= sum([int(float(df.loc[i, 'Boca Raton city, Florida!!Total!!Estimate'].replace(',', ''))) for i in range(5, 7)])
under30_pop
14270
ages_30_to_49_pop = sum([int(float(df.loc[i, 'Boca Raton city, Florida!!Total!!Estimate'].replace(',', ''))) for i in range(8, 12)])
ages_30_to_49_pop
18077
ages_50_plus = sum([int(float(df.loc[i, 'Boca Raton city, Florida!!Total!!Estimate'].replace(',', ''))) for i in range(13, 19)])
ages_50_plus
34627
total_pop = sum(int(float(df.loc[i,'Boca Raton city, Florida!!Total!!Estimate'].replace(',', ''))) for i in range(5, 20))
total_pop
83327
pop_data = \
{
'Type':['Population'],
'>30': [under30_pop],
'30-49': [ages_30_to_49_pop],
'49>': [ages_50_plus]
}
pop_df=pd.DataFrame(pop_data)
pop_df
| Type | >30 | 30-49 | 49> | |
|---|---|---|---|---|
| 0 | Population | 14270 | 18077 | 34627 |
percentage_values = [(pop_df[column].sum() / total_pop) * 100 for column in pop_df.columns if column != 'Type']
percentage_df = pd.DataFrame([percentage_values], columns=[col for col in pop_df.columns if col != 'Type'], index=['percentage'])
percentage_df.insert(0, 'Type', 'Percentage')
pop_df = pd.concat([pop_df, percentage_df])
pop_df
| Type | >30 | 30-49 | 49> | |
|---|---|---|---|---|
| 0 | Population | 14270.000000 | 18077.000000 | 34627.000000 |
| percentage | Percentage | 17.125302 | 21.694049 | 41.555558 |
pop_df.style.format({
'>30': '{:.0f}',
'30-49': '{:.0f}',
'49>': '{:.0f}',
'>30_Percentage': '{.1%}',
'30-49_Percentage': '{.1%}',
'49>_Percentage': '{.1%}',
})
| Type | >30 | 30-49 | 49> | |
|---|---|---|---|---|
| 0 | Population | 14270 | 18077 | 34627 |
| percentage | Percentage | 17 | 22 | 42 |
import warnings
warnings.filterwarnings('ignore', 'In a future version, the Index constructor will not infer numeric dtypes when passed object-dtype sequences')
# Convert the population columns to integers
pop_df.loc[pop_df['Type'] == 'Population', ['>30', '30-49', '49>']] = pop_df.loc[pop_df['Type'] == 'Population', ['>30', '30-49', '49>']].astype(int)
# Round the percentage columns to the nearest tenth
pop_df.loc[pop_df['Type'] == 'Percentage', ['>30', '30-49', '49>']] = round(pop_df.loc[pop_df['Type'] == 'Percentage', ['>30', '30-49', '49>']], 1)
pop_df
| Type | >30 | 30-49 | 49> | |
|---|---|---|---|---|
| 0 | Population | 14270.0 | 18077.0 | 34627.0 |
| percentage | Percentage | 17.1 | 21.7 | 41.6 |
# Prepare Males data
males_data = {
'>30': sum([int(df.loc[i, 'Boca Raton city, Florida!!Male!!Estimate'].replace(',', '')) for i in range(5, 8)]),
'30-49': sum([int(df.loc[i, 'Boca Raton city, Florida!!Male!!Estimate'].replace(',', '')) for i in range(8, 12)]),
'49>': sum([int(float(df.loc[i, 'Boca Raton city, Florida!!Male!!Estimate'].replace(',', ''))) for i in range(12, 20)])
}
# Add Males row to the DataFrame
pop_df.loc['Males'] = pd.Series(males_data)
# Prepare Females data
females_data = {
'>30': sum([int(df.loc[i, 'Boca Raton city, Florida!!Female!!Estimate'].replace(',', '')) for i in range(5, 8)]),
'30-49': sum([int(df.loc[i, 'Boca Raton city, Florida!!Female!!Estimate'].replace(',', '')) for i in range(8, 12)]),
'49>': sum([int(float(df.loc[i, 'Boca Raton city, Florida!!Female!!Estimate'].replace(',', ''))) for i in range(12, 20)])
}
# Add Females row to the DataFrame
pop_df.loc['Females'] = pd.Series(females_data)
# Add Females row to the DataFrame
pop_df.loc['Females'] = pd.Series(females_data)
pop_df = pop_df.rename(index={0: 'Population'})
pop_df.loc['Males', 'Type'] = 'Males'
pop_df.loc['Females', 'Type'] = 'Females'
pop_df
| Type | >30 | 30-49 | 49> | |
|---|---|---|---|---|
| Population | Population | 14270.0 | 18077.0 | 34627.0 |
| percentage | Percentage | 17.1 | 21.7 | 41.6 |
| Males | Males | 8425.0 | 7816.0 | 19938.0 |
| Females | Females | 11563.0 | 10261.0 | 25324.0 |
import seaborn as sns
import matplotlib.pyplot as plt
# Extract the required data from pop_df
data = pop_df.iloc[0:2, 1:].astype(float)
# Set the figure size
plt.figure(figsize=(10, 6))
# Plot the bar chart
sns.barplot(data=data, palette='Set3')
# Add labels and title
plt.xlabel('Age Group')
plt.ylabel('Population')
plt.title('Population by Age Group')
# Show the plot
plt.show()
import plotly.graph_objects as go
import pandas as pd
# Create a sample DataFrame
data = {
'Type': ['>30', '30-49', '49>'],
'Population': [14270, 18077, 32079],
'Percentage': [17.1, 21.7, 38.5],
'Males': [8425, 7816, 19938],
'Females': [11563, 10261, 25324]
}
df = pd.DataFrame(data)
# Create the bar chart
fig = go.Figure()
fig.add_trace(go.Bar(x=df['Type'], y=df['Population'], name='Population'))
fig.add_trace(go.Bar(x=df['Type'], y=df['Percentage'], name='Percentage'))
fig.add_trace(go.Bar(x=df['Type'], y=df['Males'], name='Males'))
fig.add_trace(go.Bar(x=df['Type'], y=df['Females'], name='Females'))
# Update the layout
fig.update_layout(
title='Population Distribution',
xaxis_title='Type',
yaxis_title='Value',
barmode='group',
legend=dict(
x=1,
y=1,
bgcolor='rgba(255, 255, 255, 0.5)',
bordercolor='rgba(0, 0, 0, 0.5)'
)
)
# Display the interactive chart
fig.show()